using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace Client
{
    public partial class MainWindow : Form
    {
        private int CurNet = -1;
        private int CurTab = -1;

        public MainWindow()
        {
            InitializeComponent();
            // this crap isnt needed now that i know what im doing!
            //menuBar.CanOverflow = true;
            //menuBar.Renderer = new ToolStripSystemRenderer();
            //menuBar.Renderer = new ToolStripProfessionalRenderer();

            tabStrip.Items.Add(new ToolStripButton("Your", null, new EventHandler(TabStripButton_Click), "tab::00"));
            tabStrip.Items.Add(new ToolStripButton("Channels", null, new EventHandler(TabStripButton_Click), "tab::01"));
            tabStrip.Items.Add(new ToolStripButton("Go", null, new EventHandler(TabStripButton_Click), "tab::02"));
            tabStrip.Items.Add(new ToolStripButton("Here", null, new EventHandler(TabStripButton_Click), "tab::03"));
            tabStrip.Items.Add(new ToolStripSeparator());
            tabStrip.Items.Add(new ToolStripButton("Private", null, new EventHandler(TabStripButton_Click), "tab::05"));
            tabStrip.Items.Add(new ToolStripButton("Query", null, new EventHandler(TabStripButton_Click), "tab::06"));
            tabStrip.Items.Add(new ToolStripButton("Windows", null, new EventHandler(TabStripButton_Click), "tab::07"));

            for(int i = 0; i < 55; ++i)
                networkStrip.Items.Add(new ToolStripButton("Network " + (i + 1), null, new EventHandler(TabStripButton_Click), "net::" + i));
            //networkStrip.Items.Add(new ToolStripButton("Network 2", null, new EventHandler(TabStripButton_Click), "net::01"));

            lagBar.Style = ProgressBarStyle.Marquee;
            bufferBar.Style = ProgressBarStyle.Marquee;

            userList.Items.Add("+[BAF64]");
            userList.Items.Add("+Sevalecan");

            //chatContent.ScrollToCaret();
            chatContent.DetectUrls = true;
            chatContent.Text = "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\ntest\nhttp://www.bafsoft.com/";
            //chatContent.AutoScrollOffset = new Point(0, -999999999);
            chatContent.SelectionStart = chatContent.TextLength;
            chatContent.ScrollToCaret();

            //CurNet = 0;
            //CurTab = 0;
            StartFlash();
        }

        public void TabStripButton_Click(object sender, EventArgs args)
        {
            ToolStripButton but = (ToolStripButton)sender;
            //MessageBox.Show(but.Name);

            string ButType = but.Name.Substring(0, but.Name.IndexOf(":"));
            int ButID = int.Parse(but.Name.Substring(but.Name.LastIndexOf(":") + 1));
            //MessageBox.Show("Type = " + ButType + "\nID = " + ButID);

            switch (ButType.ToLower())
            {
                case "net":
                    if(CurNet >= 0 && CurNet < networkStrip.Items.Count)
                        ((ToolStripButton)networkStrip.Items[CurNet]).Checked = false;
                    CurNet = ButID;
                    ((ToolStripButton)networkStrip.Items[CurNet]).Checked = true;
                    //networkStrip_UpdateSelection(networkStrip, null);
                    break;

                case "tab":
                    if(CurTab >= 0 && CurTab < tabStrip.Items.Count)
                        ((ToolStripButton)tabStrip.Items[CurTab]).Checked = false;
                    CurTab = ButID;
                    ((ToolStripButton)tabStrip.Items[CurTab]).Checked = true;
                    break;
            }
        }

        // misc
        private void MainWindow_Activated(object sender, EventArgs e)
        {
            StopFlash();
            sendText.Focus();
        }

        // window flashing
        [DllImport("user32.dll")]
        static extern bool FlashWindow(IntPtr hwnd, bool bInvert);
        bool FlashInvert = false;

        private void StartFlash()
        {
            flashTimer.Start();
        }

        private void StopFlash()
        {
            flashTimer.Stop();
        }

        private void flashTimer_Tick(object sender, EventArgs e)
        {
            FlashInvert ^= true;
            FlashWindow(this.Handle, FlashInvert);
        }

        // button handlers
        private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            (new PrefsWindow()).ShowDialog();
        }
    }
}